
/*
Post
<ussd>
                <action/> <!-1: start session 2: end session 3: send indication response-->
                <ussd_param/><!-only make sense when action is 1 or 3, should be encoded by UCS2-->
</ussd>

Get
<ussd>
                <ussd_type/><!-0: USSD notify 1: USSD-request 2: session terminated by network 3: other local client had responded 4:operation not supported 5: network timeout-->
                <ussd_str/><!-USSD string encoded by UCS2-->
</ussd>
*/
(function ($) {

    $.fn.objussd = function (InIt) {

        var timeoutCount = 0;
        var _xmlname = '';

        var action = 1;//1: start session 2: end session 3: send indication response

        this.onLoad = function (flag) {
            $("#Content").html(callProductHTML("html/home_network/ussd.html"));
            $("[id^='lt_ussd']").each(function() {
                if("input" == document.getElementById($(this).attr("id")).tagName.toLowerCase()) {
                    $(this).val(jQuery.i18n.prop($(this).attr("id")));
                } else {
                    $(this).text(jQuery.i18n.prop($(this).attr("id")));
                }
            });
            $("#lt_ussd_btnDial").click(function() {
                UssdDialUp();
            });

            $("#lt_ussd_btnCancel").click(function() {
                CanCelUssdService();
            });

            $("#lt_ussd_btnSend").click(function() {
                SendIndicationResponse();
            });

            InitUssdStatus();

        }

        function InitUssdStatus() {
            var retXml = callProductXML(_xmlname);
            var ussdType = $(retXml).find("ussd_type").text();
            if("0" == ussdType ||  "1" == ussdType) {
                $("#txtIndicationResponse").val("");
                $("#lt_ussd_txtRecvContent").text(UniDecode($(retXml).find("ussd_str").text()));
                $("#divUssdInteract").show();
                if("1" == ussdType ) {
                    $("#divIndicationResponse").show();
                    $("#lt_ussd_btnSend").show();
                } else {
                    $("#divIndicationResponse").hide();
                    $("#lt_ussd_btnSend").hide();
                }
            }
        }


        function SendIndicationResponse() {
            var mapData = new Array();
            var itemIndex = 0;
            action = 3;
            putMapElement(mapData, "RGW/ussd/action", action, itemIndex++);
            putMapElement(mapData, "RGW/ussd/ussd_param", $("#txtIndicationResponse").val(), itemIndex++);
            postXML(_xmlname, g_objXML.getXMLDocToString(g_objXML.createXML(mapData)),6000,true);
            sm("PleaseWait",150,100);
            $("#lPleaseWait").text(jQuery.i18n.prop("h1PleaseWait"));
            timeoutCount = 0;
            setTimeout(QueryUssdNetworkStatus,10000);
        }


        function CanCelUssdService() {
            var mapData = new Array();
            var itemIndex = 0;
            action = 2;
            putMapElement(mapData, "RGW/ussd/action", action, itemIndex++);
            postXML(_xmlname, g_objXML.getXMLDocToString(g_objXML.createXML(mapData)));
            $("#lt_ussd_btnDial").attr("disabled",false);
            $("#divUssdInteract").hide();
            //setTimeout(QueryUssdNetworkStatus,1000);
        }


        function UssdDialUp() {
            var mapData = new Array();
            var itemIndex = 0;
            action = 1;
            putMapElement(mapData, "RGW/ussd/action", action, itemIndex++);
            putMapElement(mapData, "RGW/ussd/ussd_param", $("#txtUssdServiceNumber").val(), itemIndex++);
            postXML(_xmlname, g_objXML.getXMLDocToString(g_objXML.createXML(mapData)),6000,true);
            sm("PleaseWait",150,100);
            $("#lPleaseWait").text(jQuery.i18n.prop("h1PleaseWait"));
            $("#lt_ussd_btnDial").attr("disabled",true);
            timeoutCount = 0;
            setTimeout(QueryUssdNetworkStatus,10000);
        }

        function QueryUssdNetworkStatus() {
            var retXml = callProductXML(_xmlname);
            var ussdType = $(retXml).find("ussd_type").text();

            if(timeoutCount > 15) {
                hm();
            }

            if("" == ussdType) {
                ++timeoutCount;
                setTimeout(QueryUssdNetworkStatus,1000);
            } else if("0" == ussdType ||  "1" == ussdType) {
                hm();
                $("#txtIndicationResponse").val("");
                $("#lt_ussd_txtRecvContent").text(UniDecode($(retXml).find("ussd_str").text()));
                $("#divUssdInteract").show();
                if("1" == ussdType ) {
                    $("#divIndicationResponse").show();
                    $("#lt_ussd_btnSend").show();
                } else {
                    $("#divIndicationResponse").hide();
                    $("#lt_ussd_btnSend").hide();
                }

            } else {
                $("#lt_ussd_btnDial").attr("disabled",false);
                $("#divUssdInteract").hide();
                if("2" == ussdType) {
                    showAlert("session terminated by network");
                } else if("3" == ussdType) {
                    showAlert("other local client had responded");
                } else if("4" == ussdType) {
                    showAlert("operation not supported");
                } else if("5" == ussdType) {
                    showAlert("network timeout");
                } else {
                    showAlert("Unkown Error");
                }
            }
        }


        this.setXMLName = function (xmlname) {
            _xmlname = xmlname;
        }

        this.onPostSuccess = function() {

        }




        return this;
    }
})(jQuery);

